package com.info.zhiduoduo.weixin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.constants.system.UserType;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.DateUtil;
import com.info.zhiduoduo.core.dto.dingdingnotice.GroupCode;
import com.info.zhiduoduo.core.service.CommonService;
import com.info.zhiduoduo.core.service.DingdingNoticeService;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.company.CompanyDetailDto;
import com.info.zhiduoduo.repository.dto.complaint.ComplaintDto;
import com.info.zhiduoduo.repository.dto.crowdsource.CrowdsourceInfoDto;
import com.info.zhiduoduo.repository.dto.maker.MakerDetailDto;
import com.info.zhiduoduo.repository.dto.partner.PartnerDetailDto;
import com.info.zhiduoduo.repository.entity.complaint.Complaint;
import com.info.zhiduoduo.repository.mapper.company.CompanyMapper;
import com.info.zhiduoduo.repository.mapper.crowdsource.CrowdsourceAccountMapper;
import com.info.zhiduoduo.repository.mapper.maker.ComplaintMapper;
import com.info.zhiduoduo.repository.mapper.maker.MakerMapper;
import com.info.zhiduoduo.repository.mapper.partner.PartnerMapper;
import com.info.zhiduoduo.weixin.formbean.complaint.ComplaintFormBean;
import com.info.zhiduoduo.weixin.formbean.complaint.ComplaintListFormBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import javax.annotation.Resource;
import javax.validation.Valid;

@Service
public class ComplaintService extends BaseService {

	@Resource
	MakerMapper makerMapper;
	@Resource
	CompanyMapper companyMapper;
	@Resource
	PartnerMapper partnerMapper;
	@Resource
	CrowdsourceAccountMapper crowdsourceAccountMapper;
	@Resource
	ComplaintMapper complaintMapper;
	@Autowired
	MakerService makerService;
	@Autowired
	DingdingNoticeService dingdingNoticeService;
	@Autowired
	CommonService commonService;

	public SmartResult submitComplaint(BaseSimpleFormBean<ComplaintFormBean> formBean) {

		int userid = formBean.getUserid();
		int idtype = formBean.getIdtype();
		ComplaintFormBean reqdata = formBean.getReqdata();
		if (reqdata == null) return errorParamSmartResult();

		String username = "";
		String phone = "";
		String type = "";
		if (idtype == UserType.MAKER || idtype == UserType.MAKER_CROWDSOURCE) {
			MakerDetailDto userInfo = makerService.getUserInfo(userid);
			if (userInfo == null) return smartResult(StatusMsg.StatusMsg_201);

			type = idtype == UserType.MAKER ? "塾人创客" : "智众包创客";
			username = userInfo.getRealname();
			phone = userInfo.getPhone();
		} else if (idtype == UserType.TAXER) {
			MakerDetailDto userInfo = makerService.getUserInfo(userid);
			if (userInfo == null) return smartResult(StatusMsg.StatusMsg_201);

			type = " 创客";
			username = userInfo.getRealname();
			phone = userInfo.getPhone();
		} else if (idtype == UserType.COMPANY) {
			CompanyDetailDto companyInfo = companyMapper.getCompanyInfo(userid);
			if (companyInfo == null) return smartResult(StatusMsg.StatusMsg_302);

			type = "企业";
			username = companyInfo.getCompname();
			phone = companyInfo.getVerifyPhone();
		} else if (idtype == UserType.PARTNER) {
			PartnerDetailDto partner = partnerMapper.getPartnerDetail(userid);
			if (partner == null) return smartResult(StatusMsg.StatusMsg_501);

			type = "合伙人";
			username = partner.getRealname();
			phone = partner.getPhone();
		} else if (idtype == UserType.CROWDSOURCE) {
			CrowdsourceInfoDto crowdsource = crowdsourceAccountMapper.getInfo(userid);
			if (crowdsource == null) return smartResult(StatusMsg.StatusMsg_601);

			type = "自然人";
			username = crowdsource.getRealname();
			phone = crowdsource.getPhone();
		} else {
			return errorParamSmartResult();
		}

		Complaint complaint = new Complaint().setUserid(userid)
				.setUserType(idtype)
				.setPhone(phone)
				.setUsername(username)
				.setTitle(reqdata.getTitle())
				.setContent(reqdata.getContent()).setCreatetime(new Date())
				.setNeedReply(reqdata.getNeedReply());
		if (complaint.insert()) {
			String content = "用户投诉\n" +
					"用户类型：" + type + "\n用户名：" + username + "\n电话：" + phone + "\n投诉标题：" + reqdata.getTitle() + "\n投诉内容：" +
					reqdata.getContent() + "\n投诉时间：" + DateUtil.getNowTimeString();
			//微企通知胡刚和tina
			new Thread(() -> commonService.enterpriseAutomaticFreezing(28,content)).start();
			new Thread(() -> commonService.enterpriseAutomaticFreezing(44,content)).start();
//			dingdingNoticeService.sendNotice(GroupCode.NORMAL, content);
			return smartResult(complaint.getId());
		}

		return optFailSmartResult();
	}

	public SmartResult<ComplaintDto> getComplaintList(BaseSimpleFormBean<ComplaintListFormBean> formBean) {
		@Valid ComplaintListFormBean reqdata = formBean.getReqdata();

		int userid = formBean.getUserid();
		int userType = formBean.getIdtype();
		String name = reqdata.getName();
		Integer needReply = reqdata.getNeedReply();
		Integer hasReply = reqdata.getHasReply();

		Page<ComplaintDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
		complaintMapper.getListByUserid(userid, userType, name, needReply, hasReply);

		return smartResultPage(page);
	}

	public SmartResult<ComplaintDto> getDetail(BaseSimpleFormBean<Integer> formBean) {
		@Valid Integer id = formBean.getReqdata();
		ComplaintDto complaintDto = complaintMapper.getDetail(id);
		if (complaintDto == null) return smartResult(StatusMsg.StatusMsg_801);
		return smartResult(complaintDto);
	}

}
